//
//  MyController.swift
//  QualityControlSystem
//
//  Created by dundun on 17/1/17.
//  Copyright © 2017年 com.mask. All rights reserved.
//

import UIKit
import UserNotifications

class MyController: UITableViewController {
    
    var titleArr: [String]?
    
        
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Color.bgGray
        tableView.alwaysBounceVertical = true
        tableView.register(UINib(nibName: "PersonInfoCell", bundle: nil), forCellReuseIdentifier: "personInfoCell")
        tableView.register(UINib.init(nibName: "MyRelatedCell", bundle: nil), forCellReuseIdentifier: "myRelatedCell")
        loadData()
    }
    
    func loadData() {
        titleArr = ["修改密码", "退出登录", "消息提醒", "关于"]
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section ==  0 {
            let personInfo = tableView.dequeueReusableCell(withIdentifier: "personInfoCell", for: indexPath) as? PersonInfoCell
            return personInfo!
        } else if indexPath.section == 3 {
            let remindMessageCell = tableView.dequeueReusableCell(withIdentifier: "myRelatedCell", for: indexPath) as? MyRelatedCell
            remindMessageCell?.setupSlider()
            remindMessageCell?.switchValueChanged = {
//                if #available(iOS 10.0, *) {
//                    let center = UNUserNotificationCenter.current()
//                } else {
//                    // Fallback on earlier versions
//                }
//                center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
//                    if granted {
//                        
//                    } else {
//                        
//                    }
//                }
//                UIApplication.shared.registerForRemoteNotifications()
            }
            remindMessageCell?.title.text = titleArr?[indexPath.section - 1]
            return remindMessageCell!
            
        } else {
            let otherCell = tableView.dequeueReusableCell(withIdentifier: "myRelatedCell", for: indexPath) as? MyRelatedCell
            otherCell?.accessoryType = .disclosureIndicator
            otherCell?.title.text = titleArr?[indexPath.section - 1]
            return otherCell!
        }
    }
    
    // MARK: - Table view delegate
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            //修改密码
        } else if indexPath.section == 2 {
            //退出登录
            let vc = UIAlertController(title: "温馨提示", message: "退出登录?", preferredStyle: .alert)
            let cancel = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let sure = UIAlertAction(title: "确定", style: .default, handler: {action in
                User.shared.clear()
                User.shared.archive()
                let vc = LoginViewController()
                self.present(vc, animated: true, completion: nil)
            })
            vc.addAction(cancel)
            vc.addAction(sure)
            present(vc, animated: true, completion: nil)
        } else if indexPath.section == 4 {
            //关于
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return Constant.MyInfoCell
        } else {
            return Constant.MyOtherCell
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.1
        } else {
            return Padding.space
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
}
